/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.com.minam.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Willian
 */
public class Util {

    private static final Logger logger = Logger.getLogger(Util.class.getName());

    // algoritmos
    public static String MD2 = "MD2";
    public static String MD5 = "MD5";
    public static String SHA1 = "SHA-1";
    public static String SHA256 = "SHA-256";
    public static String SHA384 = "SHA-384";
    public static String SHA512 = "SHA-512";

    private static String toHexadecimal(byte[] digest) {
        String hash = "";
        for (byte aux : digest) {
            int b = aux & 0xff;
            if (Integer.toHexString(b).length() == 1) {
                hash += "0";
            }
            hash += Integer.toHexString(b);
        }
        return hash;
    }

    public static String getStringMessageDigest(String message, String algorithm) {
        byte[] digest = null;
        byte[] buffer = message.getBytes();
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            messageDigest.reset();
            messageDigest.update(buffer);
            digest = messageDigest.digest();
        } catch (NoSuchAlgorithmException ex) {
            System.out.println("Error creando Digest");
        }
        return toHexadecimal(digest);
    }

    /*
     * Enviar Mail normal => Asunto,Destinatario,Mensaje
     */
    public static boolean EnviarMailNormal(String destino, String asunto, String msj) {
        boolean status = true;
        String correo = PropertiesInterno.CORREO_ENVIO;
        String password = PropertiesInterno.PASS_ENVIO;
        
        // Propiedades de la conexión
        Properties props = new Properties();
        props.setProperty("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.starttls.enable", "true");
        props.setProperty("mail.smtp.port", "587");
        props.setProperty("mail.smtp.user", correo);
        props.setProperty("mail.smtp.password", password);
        props.setProperty("mail.smtp.auth", "true");

        // Preparamos la sesion
        Session session = Session.getDefaultInstance(props);

        // Construimos el mensaje
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(correo));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(destino));
            message.setSubject(asunto);
            message.setText(msj, "utf-8", "html"); // Formato HTML
            Transport t = session.getTransport("smtp");
            t.connect(correo,password);
            t.sendMessage(message, message.getAllRecipients());
            t.close();
            logger.log(Level.INFO, "Mail enviado, se adjunta datos: {0} \n{1}", new Object[]{destino, asunto});
        } catch (Exception ex) {
            logger.log(Level.WARNING, "EnviarMailNormal: Error[{0}] al enviar Mail, se adjunta datos => {1} \n{2} \n{3}", new Object[]{ex.getLocalizedMessage(), destino, asunto, msj});
            status = false;
        }
        return status;
    }
}
